home *** CD-ROM | disk | FTP | other *** search
/ Aminet 2 / Aminet AMIGA CDROM (1994)(Walnut Creek)[Feb 1994][W.O. 44790-1].iso / Aminet / dev / c / RConfig.lha / RConfig_v1.1 / src / genlib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-20  |  11.4 KB  |  363 lines

  1. /*
  2.  * RConfig -- Replacement Library Configuration
  3.  *   Copyright 1992 by Anthon Pang, Omni Communications Products
  4.  *
  5.  * Source File: genlib.c
  6.  * Description: Library generator
  7.  * Comments: Calls external 'cc' to compile source with appropriate flags;
  8.  *   calls external 'lb' to create library module
  9.  */
  10.  
  11. #include <exec/types.h>
  12. #include <dos/dos.h>
  13. #include <dos/dosextens.h>
  14. #include <intuition/intuition.h>
  15. #include <libraries/gadtools.h>
  16. #include <libraries/asl.h>
  17. #include <clib/intuition_protos.h>
  18. #include <clib/gadtools_protos.h>
  19. #include <clib/exec_protos.h>
  20. #include <stdio.h>
  21. #include <time.h>
  22. #include "rc2.h"
  23.  
  24. extern UWORD WaitPointerImage[];
  25.  
  26. extern void ClearMsgPort(struct MsgPort *mport);
  27.  
  28. extern int alloca_mode;
  29. extern int setjmp_mode;
  30. extern int main_mode;
  31. extern int stkchk_mode;
  32.  
  33. extern int integer_mode;    /* 32 bits vs 16 bits */
  34. extern int data_mode;       /* small model vs large model */
  35.  
  36. extern ULONG ccopts[10];    /* ccopts values */
  37. extern int uservars;        /* precedence for user specified register vars */
  38.  
  39. extern struct FileRequester *fr;
  40.  
  41. #define THRESHHOLD  2768    /* min. threshhold */
  42. #define STACKSIZE   8192    /* must be greater than threshhold */
  43. #define CONTEXTSIZE 128     /* arbitrary minimum */
  44.  
  45. char cmdbuffer[2048];
  46. char optbuffer[2048];
  47. char num[36];
  48.  
  49. char currentdir[2048] ="\0"; /* defaults to current directory */
  50.  
  51. /* not configurable yet */
  52. #define CRT0SRC     "crt0.a68"
  53. #define CRT0OBJ     "crt0.o"
  54. #define MAINSRC     "_main.c"
  55. #define MAINOBJ     "_main.o"
  56. #define EXITSRC     "_exit.c"
  57. #define EXITOBJ     "_exit.o"
  58. #define RLIBSRC     "rlib.c"
  59. #define RLIBOBJ     "rlib.o"
  60. #define OUTPUTNAME  "rlib.lib"
  61. #define RLIBH       "rlib.h"
  62.  
  63. #define RESETVALUE(g,v) GT_SetGadgetAttrs(RConfigGadgets[g], \
  64.       RConfigWnd, NULL, GTIN_Number, v, TAG_END)
  65.  
  66. #define WRITE_SOPT(g,o) \
  67.     if (ccopts[g]) \
  68.         strcat(optbuffer, "-s" o " "); /* enable */    \
  69.     else                                        \
  70.         strcat(optbuffer, "-s0" o " ") /* disable */
  71.  
  72. #define WRITE_ROPT(g,o) \
  73.     if (ccopts[g]) \
  74.         strcat(optbuffer, "-r" o " "); /* enable */    \
  75.     else                                        \
  76.         strcat(optbuffer, "-r0" o " ") /* disable */
  77.  
  78. #define WRITEDEF(d) \
  79.     fprintf(rh, "#ifndef %s\n" \
  80.           "#define %s\n" \
  81.           "#endif\n\n", d, d)
  82.  
  83. struct EasyStruct myES = {
  84.     sizeof(struct EasyStruct),
  85.     0,
  86.     (UBYTE *)"RConfig System Error",
  87.     (UBYTE *)"%s",
  88.     (UBYTE *)"Continue"
  89. };
  90.  
  91. static char *paths[2] = {
  92.    "RLIB:",
  93.    "AZTEC:RLIB/"
  94. };
  95.  
  96. void GenLib() {
  97.     long int minstk, stksize, context;
  98.     long ok;
  99.     struct Process *pp;
  100.     APTR savedwindowptr;
  101.     struct Requester waitRequest;
  102.     int directory = -1; /* -1 == none, 0 == "RLIB:", 1 == "AZTEC:RLIB" */
  103.     FILE *rh = NULL;
  104.     time_t currenttime;
  105.     extern int pathlength;
  106.  
  107.     InitRequester(&waitRequest); /* block input to this window */
  108.     if (Request(&waitRequest, RConfigWnd))
  109.         SetPointer(RConfigWnd, WaitPointerImage, 16, 16, -6, 0);
  110.     else {
  111.         EasyRequest(NULL, &myES, NULL, "Unable to initialize requester.");
  112.         return;
  113.     }
  114.  
  115.     SetWindowTitles(RConfigWnd, (UBYTE*)"Generating library...", (UBYTE*)-1);
  116.  
  117.     /* verify currentdir */
  118.     if (pathlength) {
  119.         sprintf(currentdir, "%s", fr->rf_Dir);
  120.         if (currentdir[pathlength-1] != ':' &&
  121.               currentdir[pathlength-1] != '/')
  122.             strcat(currentdir, "/");
  123.         if (access(currentdir, 0)) {
  124.             EasyRequest(NULL, &myES, NULL, "Output directory not found.");
  125.             goto GenLibFail;
  126.         }
  127.     } else
  128.         currentdir[0] = '\0';
  129.     
  130.     sprintf(cmdbuffer, "%s" RLIBH, currentdir);
  131.     rh = fopen(cmdbuffer, "w");
  132.     if (!rh) {
  133.         EasyRequest(NULL, &myES, NULL, "Unable to create \"rlib.h\".");
  134.         goto GenLibFail;
  135.     } else {
  136.         currenttime = time(NULL); /* or time(¤ttime); */
  137.         fprintf(rh,
  138.               "/*\n" \
  139.               " *  lib.h\n" \
  140.               " *\n" \
  141.               " *  Header file for rlib.lib created %s",
  142.               ctime(¤ttime)
  143.         );
  144.     }
  145.  
  146.     if (getenv("manx/include")==NULL) {
  147.         EasyRequest(NULL, &myES, NULL, "Manx INCLUDE environment variable not set.");
  148.         goto GenLibFail;
  149.     }
  150.  
  151.     if (stkchk_mode & 2) {
  152.         minstk = ((struct StringInfo *)RConfigGadgets[GD_STKCHK_MINSTK]->SpecialInfo)->LongInt;
  153.         stksize = ((struct StringInfo *)RConfigGadgets[GD_STKCHK_STKSIZE]->SpecialInfo)->LongInt;
  154.         context = ((struct StringInfo *)RConfigGadgets[GD_STKCHK_CONTEXT]->SpecialInfo)->LongInt;
  155.  
  156.         if (minstk < THRESHHOLD) {
  157.             RESETVALUE(GD_STKCHK_MINSTK, THRESHHOLD);
  158.             EasyRequest(NULL, &myES, NULL, "Threshhold < minimum (2768).");
  159.             goto GenLibFail;
  160.         }
  161.  
  162.         if (stksize < STACKSIZE) {
  163.             RESETVALUE(GD_STKCHK_STKSIZE, STACKSIZE);
  164.             EasyRequest(NULL, &myES, NULL, "Stack size < minimum (8192).");
  165.             goto GenLibFail;
  166.         } else if (stksize < minstk) {
  167.             RESETVALUE(GD_STKCHK_STKSIZE, minstk);
  168.             EasyRequest(NULL, &myES, NULL, "Stack size < Threshhold.");
  169.             goto GenLibFail;
  170.         }    
  171.     
  172.         if (context < CONTEXTSIZE) {
  173.             RESETVALUE(GD_STKCHK_CONTEXT, CONTEXTSIZE);
  174.             EasyRequest(NULL, &myES, NULL, "Context size < minimum (128).");
  175.             goto GenLibFail;
  176.         } else if (context > stksize) {
  177.             RESETVALUE(GD_STKCHK_CONTEXT, CONTEXTSIZE);
  178.             EasyRequest(NULL, &myES, NULL, "Context size > Stack size.");
  179.             goto GenLibFail;
  180.         }
  181.     }
  182.  
  183.     if (main_mode & 2) {
  184.         if (main_mode & 1) {
  185.             if (data_mode) {
  186.                 EasyRequest(NULL, &myES, NULL, "Resident main/exit not permitted in large data model.");
  187.                 goto GenLibFail;
  188.             }
  189.             fprintf(rh, " *   - %s main/exit\n", "resident");
  190.         } else {
  191.             fprintf(rh, " *   - %s main/exit\n", "detach");
  192.         }
  193.     }
  194.  
  195.     pp = (struct Process *)FindTask(0L);
  196.     savedwindowptr = pp->pr_WindowPtr;
  197.     pp->pr_WindowPtr = (APTR)-1; /* suppress system requesters */
  198.  
  199.     if (access(paths[0], 0)==0) /* RLIB: exists */
  200.         directory = 0;
  201.     else if (access(paths[1], 0)==0) /* AZTEC:RLIB exists */
  202.         directory = 1;
  203.     else {
  204.         pp->pr_WindowPtr = savedwindowptr; /* restore original value */
  205.         EasyRequest(NULL, &myES, NULL, "RConfig Library Source Not Found.");
  206.         goto GenLibFail;
  207.     }
  208.  
  209.     if (access(OUTPUTNAME, 0)==0) /* rlib.lib already exists */
  210.         remove(OUTPUTNAME);
  211.  
  212.     pp->pr_WindowPtr = savedwindowptr; /* restore original value */
  213.  
  214.     optbuffer[0] = '\0';
  215.  
  216.     if (alloca_mode & 2) {
  217.         sprintf(optbuffer, "%s", "-d__ALLOCA_REPLACE ");
  218.         if (alloca_mode & 1) {
  219.             fprintf(rh, " *   - %s alloca\n", "risky");
  220.             strcat(optbuffer, "-d__RISKY_ALLOCA ");
  221.         } else {
  222.             fprintf(rh, " *   - %s alloca\n", "safe");
  223.             strcat(optbuffer, "-d__SAFE_ALLOCA ");
  224.         }
  225.     }
  226.  
  227.     if (stkchk_mode & 2) {
  228.         strcat(optbuffer, "-d__STKCHK_REPLACE ");
  229.         sprintf(num, "-dSTKCHK_MIN_STACK=%d ", minstk);
  230.         strcat(optbuffer, num);
  231.         if (stkchk_mode & 1) {
  232.             fprintf(rh, " *   - dynastack stkchk, threshhold=%ld, new stack=%ld, context=%ld\n", minstk, stksize, context);
  233.             strcat(optbuffer, "-d__DYNASTACK_STKCHK ");
  234.             sprintf(num, "-dSTKCHK_STACK_SIZE=%d ", stksize);
  235.             strcat(optbuffer, num);
  236.             sprintf(num, "-dSTKCHK_CONTEXT_SIZE=%d ", context);
  237.             strcat(optbuffer, num);
  238.         } else {
  239.             fprintf(rh, " *   - better stkchk, threshhold=%ld\n", minstk);
  240.             strcat(optbuffer, "-d__BETTER_STKCHK ");
  241.         }
  242.     }
  243.  
  244.     if (setjmp_mode) {
  245.         fputs(" *   - setjmp/longjmp\n", rh);
  246.         strcat(optbuffer, "-d__SETJMP_REPLACE ");
  247.     }
  248.  
  249.     fputs(" */\n\n", rh);
  250.  
  251.     if (stkchk_mode & 2 && setjmp_mode)
  252.         WRITEDEF("__DYNASTACK_STKCHK");
  253.  
  254.     if (alloca_mode & 2 && !(alloca_mode & 1))
  255.         WRITEDEF("__SAFE_ALLOCA");
  256.  
  257.     if (alloca_mode & 2) {
  258.         if (setjmp_mode) {
  259.             WRITEDEF("__ALLOCA_REPLACE");
  260.         }
  261.         fputs("#include <alloca.h>\n", rh);
  262.     }
  263.  
  264.     if (setjmp_mode)
  265.         fputs("#include <rsetjmp.h>\n", rh);
  266.  
  267.     if (data_mode)
  268.         strcat(optbuffer, "-md ");  /* large data memory model */
  269.     else
  270.         strcat(optbuffer, "-m0d "); /* small data memory model */
  271.  
  272.     if (integer_mode)
  273.         strcat(optbuffer, "-ps "); /* 16 bit ints */
  274.     else            
  275.         strcat(optbuffer, "-pl "); /* 32 bit ints */
  276.  
  277.     /* optimization flags here override CCOPTS env. var. setting */
  278.     WRITE_SOPT(GD_sa, "a");
  279.     WRITE_SOPT(GD_sb, "b");
  280.     WRITE_SOPT(GD_sf, "f");
  281.     WRITE_SOPT(GD_sm, "m");
  282.     WRITE_SOPT(GD_ss, "s");
  283.     WRITE_SOPT(GD_sp, "p");
  284.     WRITE_SOPT(GD_sn, "n");
  285.     WRITE_ROPT(GD_r4, "4");
  286.     WRITE_ROPT(GD_r6, "6");
  287.  
  288.     if (ccopts[GD_srsu]) {
  289.         if (uservars)
  290.             strcat(optbuffer, "-su");
  291.         else
  292.             strcat(optbuffer, "-sr");
  293.     } else
  294.         strcat(optbuffer, "-s0r -s0u"); /* disable */
  295.  
  296.     sprintf(cmdbuffer, "cc %s%s -i%s -oT:%s ",
  297.           paths[directory], RLIBSRC, paths[directory], RLIBOBJ);
  298.  
  299.     strcat(cmdbuffer, optbuffer);
  300.     putchar('\n'); puts(cmdbuffer);
  301.     ok = SystemTags(cmdbuffer, TAG_END);        
  302.  
  303.     if (main_mode & 2) {
  304.         if (main_mode & 1) {
  305.             sprintf(cmdbuffer, "as %s%s -o T:%s -e__RESSTART_MAIN",
  306.                   paths[directory], CRT0SRC, CRT0OBJ);
  307.             putchar('\n'); puts(cmdbuffer);
  308.             ok = SystemTags(cmdbuffer, TAG_END);        
  309.             sprintf(cmdbuffer, "cc %s%s -oT:%s -d__RESSTART_MAIN ",
  310.                   paths[directory], EXITSRC, EXITOBJ);
  311.             strcat(cmdbuffer, optbuffer);
  312.             putchar('\n'); puts(cmdbuffer);
  313.             ok = SystemTags(cmdbuffer, TAG_END);
  314.             sprintf(cmdbuffer, "lb %s%s T:%s T:%s T:%s",
  315.                   currentdir, OUTPUTNAME, CRT0OBJ, EXITOBJ, RLIBOBJ);
  316.             putchar('\n'); puts(cmdbuffer);
  317.             ok = SystemTags(cmdbuffer, TAG_END);
  318.             remove("T:_exit.o"); remove("T:crt0.o");
  319.         } else {
  320.             sprintf(cmdbuffer, "as %s%s -o T:%s",
  321.                   paths[directory], CRT0SRC, CRT0OBJ);
  322.             putchar('\n'); puts(cmdbuffer);
  323.             ok = SystemTags(cmdbuffer, TAG_END);        
  324.             /* detach code */
  325.             sprintf(cmdbuffer, "cc %s%s -oT:%s -d__DETACH_MAIN -m0b ",
  326.                   paths[directory], MAINSRC, MAINOBJ);
  327.             strcat(cmdbuffer, optbuffer);
  328.             putchar('\n'); puts(cmdbuffer);
  329.             ok = SystemTags(cmdbuffer, TAG_END);
  330.             sprintf(cmdbuffer, "lb %s%s T:%s T:%s T:%s",
  331.                   currentdir, OUTPUTNAME, CRT0OBJ, MAINOBJ, RLIBOBJ);
  332.             putchar('\n'); puts(cmdbuffer);
  333.             ok = SystemTags(cmdbuffer, TAG_END);
  334.             remove("T:_main.o"); remove("T:crt0.o");
  335.         }
  336.     } else {
  337.         sprintf(cmdbuffer, "lb %s%s T:%s",
  338.               currentdir, OUTPUTNAME, RLIBOBJ);
  339.         putchar('\n'); puts(cmdbuffer);
  340.         ok = SystemTags(cmdbuffer, TAG_END);
  341.     }
  342.     remove("T:rlib.o");
  343.  
  344.     goto GenLibQuit;
  345.  
  346. GenLibFail:
  347.     /* just falls through */
  348.  
  349. GenLibQuit:
  350.     if (rh)
  351.         fclose(rh);
  352.  
  353.     SetWindowTitles(RConfigWnd, (UBYTE*)"Replacement Library Configuration", (UBYTE*)-1);
  354.  
  355.     DisplayBeep(NULL); /* all screens...may want to change
  356.                           this to just the Workbench screen */
  357.  
  358.     ClearMsgPort(RConfigWnd->UserPort);
  359.  
  360.     ClearPointer(RConfigWnd);
  361.     EndRequest(&waitRequest, RConfigWnd);
  362. }
  363.